package com.ruoyi.system.service.impl;

import java.math.BigDecimal;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.enums.StatusEnum;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.SysAgent;
import com.ruoyi.system.domain.SysOrder;
import com.ruoyi.system.domain.vo.MemberVo;
import com.ruoyi.system.domain.vo.OrderVo;
import com.ruoyi.system.mapper.SysAgentMapper;
import com.ruoyi.system.mapper.SysOrderMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysMemberMapper;
import com.ruoyi.system.domain.SysMember;
import com.ruoyi.system.service.ISysMemberService;
import com.ruoyi.common.core.text.Convert;

/**
 * 会员管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-10-08
 */
@Service
public class SysMemberServiceImpl implements ISysMemberService 
{
    @Autowired
    private SysMemberMapper sysMemberMapper;

    @Autowired
    private SysOrderMapper sysOrderMapper;

    @Autowired
    private SysAgentMapper sysAgentMapper;
    /**
     * 查询会员管理
     * 
     * @param id 会员管理主键
     * @return 会员管理
     */
    @Override
    public SysMember selectSysMemberById(Long id)
    {
        return sysMemberMapper.selectSysMemberById(id);
    }

    /**
     * 查询会员管理列表
     * 
     * @param sysMember 会员管理
     * @return 会员管理
     */
    @Override
    public List<SysMember> selectSysMemberList(SysMember sysMember)
    {
        sysMember.setStatus(StatusEnum.ENABLE.getCode());
        return sysMemberMapper.selectSysMemberList(sysMember);
    }

    @Override
    public List<MemberVo> selectMemberList(SysMember sysMember) {
        sysMember.setStatus(StatusEnum.ENABLE.getCode());
        List<SysMember> sysMembers = sysMemberMapper.selectSysMemberList(sysMember);
        return sysMembers.stream().map(member -> {
            MemberVo memberVo = new MemberVo();
            BeanUtils.copyProperties(member, memberVo);
            if (Objects.nonNull(member.getLevel1Id()) && member.getLevel1Id() > 0){
                memberVo.setLevel1Name(sysAgentMapper.selectSysAgentById(member.getLevel1Id()).getName());
            }
            if (Objects.nonNull(member.getLevel2Id()) && member.getLevel2Id() > 0){
                memberVo.setLevel2Name(sysAgentMapper.selectSysAgentById(member.getLevel2Id()).getName());
            }
            LambdaQueryWrapper<SysOrder> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(SysOrder::getMobile, member.getMobile());
            List<SysOrder> sysOrders = sysOrderMapper.selectList(queryWrapper);
            memberVo.setOrderList(sysOrders.stream().map(order -> {
                OrderVo orderVo = new OrderVo();
                BeanUtils.copyProperties(order, orderVo);
                return orderVo;
            }).collect(Collectors.toList()));
            memberVo.setTotalAmount(sysOrders.stream().map(SysOrder::getPayAmount).reduce(BigDecimal::add).orElse(BigDecimal.ZERO));
            return memberVo;
        }).collect(Collectors.toList());
    }

    /**
     * 新增会员管理
     * 
     * @param sysMember 会员管理
     * @return 结果
     */
    @Override
    public int insertSysMember(SysMember sysMember)
    {
        LambdaQueryWrapper<SysMember> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysMember::getMobile, sysMember.getMobile()).eq(SysMember::getName, sysMember.getName());
        Long count = sysMemberMapper.selectCount(queryWrapper);
        if (count > 0) {
            throw new ServiceException("用户名和手机号已存在");
        }
        sysMember.setCreateTime(DateUtils.getNowDate());
        return sysMemberMapper.insertSysMember(sysMember);
    }

    /**
     * 修改会员管理
     * 
     * @param sysMember 会员管理
     * @return 结果
     */
    @Override
    public int updateSysMember(SysMember sysMember)
    {
        SysMember old = sysMemberMapper.selectSysMemberById(sysMember.getId());
        if (!old.getMobile().equals(sysMember.getMobile()) || !old.getName().equals(sysMember.getName())) {
            LambdaQueryWrapper<SysMember> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(SysMember::getMobile, sysMember.getMobile()).eq(SysMember::getName, sysMember.getName());
            Long count = sysMemberMapper.selectCount(queryWrapper);
            if (count > 0){
                throw new ServiceException("用户名和手机号已存在");
            }
        }
        sysMember.setUpdateTime(DateUtils.getNowDate());
        return sysMemberMapper.updateSysMember(sysMember);
    }

    /**
     * 批量删除会员管理
     * 
     * @param ids 需要删除的会员管理主键
     * @return 结果
     */
    @Override
    public int deleteSysMemberByIds(String ids)
    {
        return sysMemberMapper.deleteSysMemberByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除会员管理信息
     * 
     * @param id 会员管理主键
     * @return 结果
     */
    @Override
    public int deleteSysMemberById(Long id)
    {
        return sysMemberMapper.deleteSysMemberById(id);
    }
}
